package com.veepeak.server.common;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * Created by mabin on 16/6/15.
 */
public abstract class BaseService<T,ID extends Serializable> {

    public abstract BaseRepository<T,ID> getRepo();

    public T save(T t){
        return getRepo().save(t);
    }

    public T get(ID id){
        return getRepo().getOne(id);
    }

    public void delete(T t){
        getRepo().delete(t);
    }

    public void delete(ID id){
        getRepo().deleteById(id);
    }

    public Page<T> findAll(Pageable pageable){
        return getRepo().findAll(pageable);
    }

    public List<T> findAll(Iterable<ID> ids){
        return getRepo().findAllById(ids);
    }

    public List<T> findAll(){
        return getRepo().findAll();
    }

    public Optional<T> get(Example<T> example){
        return getRepo().findOne(example);
    }

    public List<T> findAll(Example<T> example){
        return getRepo().findAll(example);
    }
}